package com.axend.aerosense.login.viewmodel;

import android.app.Activity;
import android.text.TextUtils;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.bean.TipsBean;
import com.axend.aerosense.common.bean.UpdataPasswordBean;
import com.axend.aerosense.common.bean.VerificationBean;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.CheckUtil;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.login.R;
import com.axend.aerosense.login.bean.LoginResultBean;
import com.axend.aerosense.login.utils.LoginRegistUtil;
import com.axend.aerosense.base.impl.ISimpleAction;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.SimpleViewModel;
import com.axend.aerosense.common.bean.TitleBarBean;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.disposables.Disposable;


public class LoginUserInfoViewModel extends SimpleViewModel<UserInfo> {
    private Disposable disposable;
    public MutableLiveData<String> userEmail = new MutableLiveData<>("");
    public MutableLiveData<String> password = new MutableLiveData<>("");
    public MutableLiveData<String> vcode = new MutableLiveData<>("");
    public MutableLiveData<Boolean> isEyeOpen = new MutableLiveData<>(false);  //小眼睛状态
    public MutableLiveData<Boolean> isChecked = new MutableLiveData<>(false);  //用户协议是否勾选
    public MutableLiveData<String> errInfo = new MutableLiveData<>("");

    public LoginUserInfoViewModel(ISimpleAction<UserInfo> simpleActionImpl) {
        super(simpleActionImpl);
    }

    /** 清空电话号码 */
    public void clearEmail(){
        userEmail.setValue("");
        errInfo.setValue("");
    }

    /** 清空电话号码 */
    public void clearPassword(){
        password.setValue("");
        errInfo.setValue("");
    }

    public void changeEye(){
        Boolean b = isEyeOpen.getValue();
        isEyeOpen.postValue(!b);
    }

    public void changeCheck(){
        Boolean b = isChecked.getValue();
        isChecked.postValue(!b);
    }

    @Override
    public void onLoad() {

    }

    public void onRegister(Activity activity){
        disposable = LoginRegistUtil.getInstance().onRegister(activity, userEmail.getValue(), password.getValue(), vcode.getValue(), new ISimpleAction<LoginResultBean>() {
            @Override
            public void onSuccess(LoginResultBean loginResultBean) {
                MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.TOKEN, loginResultBean.getToken());
                disposable = LoginRegistUtil.getInstance().getRoomType(activity, loginResultBean.getToken(), ()->{
                    MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.LOGIN_EMAIL, userEmail.getValue());
                    MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.TOKEN, loginResultBean.getToken());
                    MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.USR_INFO, loginResultBean.getUserInfo());
                    MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.IMAGE_ICON, loginResultBean.getUserInfo().userImg);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.USR_INFO_CHANGE, UserInfo.class).postValue(loginResultBean.getUserInfo());
                    errInfo.setValue("");
                    simpleActionImpl.onSuccess(loginResultBean.getUserInfo());
                });
            }

            @Override
            public void onFail(String decs) {
                errInfo.setValue(decs);
                simpleActionImpl.onFail(decs);
            }
        });
    }

    public void onLogin(Activity activity){
        disposable = LoginRegistUtil.getInstance().onLogin(activity, userEmail.getValue(), password.getValue(), new ISimpleAction<LoginResultBean>() {
            @Override
            public void onSuccess(LoginResultBean loginResultBean) {
                MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.TOKEN, loginResultBean.getToken());
                MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.LOGIN_EMAIL, userEmail.getValue());
                MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.USR_INFO, loginResultBean.getUserInfo());
                MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.IMAGE_ICON, loginResultBean.getUserInfo().userImg);
//                disposable = LoginRegistUtil.getInstance().getRoomType(activity, loginResultBean.getToken(), ()->{
//                    errInfo.setValue("");
//                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.USR_INFO_CHANGE, UserInfo.class).postValue(loginResultBean.getUserInfo());
//                    simpleActionImpl.onSuccess(loginResultBean.getUserInfo());
//                });

                LiveDatabus.getInstance().with(Gobal.EVENT_KEY.USR_INFO_CHANGE, UserInfo.class).postValue(loginResultBean.getUserInfo());
                errInfo.setValue("");
                simpleActionImpl.onSuccess(loginResultBean.getUserInfo());
            }

            @Override
            public void onFail(String decs) {
                errInfo.setValue(decs);
                simpleActionImpl.onFail(decs);
            }
        });
    }

    public void onUserCenterChangePassword(Activity activity, IOperationResult operationResult){
        String mail = userEmail.getValue();
        if(TextUtils.isEmpty(mail)){
            UserInfo userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
            if(null == userInfo || TextUtils.isEmpty(userInfo.userEmail)){
                mail = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.LOGIN_EMAIL);
                if(TextUtils.isEmpty(mail)){
                    return;
                }
            }else{
                mail = userInfo.userEmail;
            }

            userEmail.setValue(mail);
        }
        onUpdataPassword(activity, operationResult);
    }

    public void onUpdataPassword(Activity activity, IOperationResult operationResult){
        disposable = LoginRegistUtil.getInstance().onUpdataPassword(activity, userEmail.getValue(), password.getValue(), vcode.getValue(), new ISimpleAction<String>() {
            @Override
            public void onSuccess(String string) {
                errInfo.setValue("");
                if(null != operationResult){
                    operationResult.onSuccess();
                }
            }

            @Override
            public void onFail(String decs) {
                errInfo.setValue(decs);
                if(null != operationResult){
                    operationResult.onFail();
                }
            }
        });
    }

    public void onUserCenterEmailVCode(Activity activity, VerificationBean.ActionType actionType, IOperationResult operationResult){
        String mail = userEmail.getValue();
        if(TextUtils.isEmpty(mail)){
            UserInfo userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
            if(null == userInfo || TextUtils.isEmpty(userInfo.userEmail)){
                mail = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.LOGIN_EMAIL);
                if(TextUtils.isEmpty(mail)){
                    return;
                }
            }else{
                mail = userInfo.userEmail;
            }

            userEmail.setValue(mail);
        }
        onEmailVCode(activity, actionType, operationResult);
    }
    
    private long clickTime = 0;

    public synchronized void onEmailVCode(Activity activity, VerificationBean.ActionType actionType, IOperationResult operationResult){
        long tmp = System.currentTimeMillis();
        if(tmp - clickTime < 2000){
            return;
        }
        clickTime = tmp;
        disposable = LoginRegistUtil.getInstance().getEmailVCode(activity, userEmail.getValue(), actionType, operationResult);
    }


    @Override
    protected void onCleared() {
        if(null != disposable && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        super.onCleared();
    }
}
